<?php
namespace Admin\Controller;
use Common\Controller\AdminBaseController;

class AdminController extends AdminBaseController {

    //系统用户列表-
    public function adminlist(){
        $page['page'] = abs(I('path.2',1));//当前页
        $num = 10; //每页几条数据
        $admin = M("admin");
        $data = $admin->order('id')->limit(($page['page']-1)*$num,$num)->select();
        $page['count'] = $admin->count(); //总条数
        $page['pages'] = ceil($page['count']/$num);//共几页
        for ($i=0; $i < count($data); $i++) {
            $id = $data[$i]['id'];
            $data[$i]['groupname']=$this->getgrouptitle($this->getusergroup($id));
        }
        //获取用户组
        $group = M("auth_group");
        $groups = $group->where('status=1')->select();
        $this->assign('admin',$data);
        $this->assign('group',$groups);
        $this->assign('page',$page);
        $this->display("Adminlte/pages/tables/adminlist");
    }

    //开启禁用系统用户
    public function state(){
        $id = I('path.2');
        $user = M("admin");
        $state = $user->where('id='.$id)->getField('status');
        $tosta = abs($state-1);
        $res = $this->statusadminbyid($id,$tosta);
        if ($res) {
            $this->ajaxReturn("1");
        }
    }

    //ajax获取用户的用户组ID
    public function admingroupidajax(){
        $id = I('path.2');
        $groupid = $this->getusergroup($id);
        $this->ajaxReturn($groupid);
    }

    //新建系统用户
    public function addadmin(){
        $data['username'] = I("post.username");
        $data['password'] = I("post.password");
        $data['checkpw'] = I("post.checkpw");
        $user['group_id'] = I("post.group_id");
        $admin = D("Admin");
        if ($admin->create($data)){
             $adminid = $admin->add();
             if ($adminid) { //关联用户组
               $group = M("auth_group_access");
               $acces['uid'] =$adminid;
               $acces['group_id'] =$user['group_id'];
               $res = $group->add($acces);
               if ($res) {
                   $this->ajaxReturn("1");
               }
            }
        }
    }

    //修改用户
    public function editadmin(){
        $sta = '1';
        if (I('post.username')) {
            $data['username'] = I('post.username');
        }
        $data['id'] = I('post.userid');
        if (I('post.password')) {
            $data['password'] = I('post.password');
            $data['checkpw'] = I('post.checkpw');
        }
        $groupid = I('post.group_id');
        $admin = D("Admin");
        if ($admin->create($data)){
            $result = $admin->save();
            if (!$result) {
                $sta = '0';
            }
        }else{
            $sta = '0';
        }
        
        //修改用户组id
        if ($groupid != $this->getusergroup($data['id'])) {
            $group = M("auth_group_access");
            $res = $group->where('uid='.$data['id'])->setField('group_id',$groupid);
            if ($res) {
                $sta = '1';
            }else{
                $sta = '0';
            }
        }
        $this->ajaxReturn($sta);
    }

    //删除用户
    public function deladmin(){
        $data['id'] = I('post.id');
        $user = M("Admin");
        $res = $user->where('id='.$data['id'])->delete();
        if ($res) {
            $this->ajaxReturn("1");
        }
    }

    //用户组列表
    public function authgroup(){
        $page['page'] = abs(I('path.2',1));//当前页
        $num = 10; //每页几条数据
        $group = M("auth_group");
        $data = $group->order('id')->limit(($page['page']-1)*$num,$num)->select();
        $class = $this->getallclass();

        $page['count'] = $group->count(); //总条数
        $page['pages'] = ceil($page['count']/$num);//共几页
        for ($i=0; $i < count($class); $i++) { 
            $class[$i]['rules']= $this->getstrulesbyclassid($class[$i]['classid']);
        }
        $this->assign('group',$data);
        $this->assign('class',$class);
        $this->assign('page',$page);
        $this->display("Adminlte/pages/tables/authgroup");
    }

    //新建用户组
    public function addgroup(){
        $data['title'] = I("post.title");
        $data['rules'] = I("post.rules");
        sort($data['rules']);
        $data['rules'] = implode(',', $data['rules']);
        $rules = M("auth_group");
        $rules->create($data);
        $res = $rules->add();
        if ($res) {
            $this->ajaxReturn("1");
        }
    }

    //修改用户组
    public function editgroup(){
        $data['id'] = I("post.id");
        $data['title'] = I("post.title");
        $rularr = I("post.rule");
        sort($rularr);
        $data['rules'] = implode(',', $rularr);
        $rules = M("auth_group");
        $result = $rules->where('id='.$data['id'])->save($data);
        if ($result) {
            $this->ajaxReturn("1");
        }
    }

    //开启禁用用户组
    public function groupstate(){
        $groupid = I("post.id");
        $group = M("auth_group");
        $groupstate = $group->where('id='.$groupid)->getField('status');
        $togpstate = abs($groupstate-1);
        $adminarr = $this->getadminarrbygroupid($groupid);
        $result = $group->where('id='.$groupid)->setField('status',$togpstate);
        if ($result) {
            foreach ($adminarr as $admid) {
                $this->statusadminbyid($admid,$togpstate);
            }
            $this->ajaxReturn("1");
        }
    }

    //删除用户组
    public function delgroup(){
        $groupid = I("post.id");
        $result = $this->delgroupbyid($groupid);
        if ($result) {
            $this->ajaxReturn("1");
        }
    }
    
    //获取所有用户组
    private function getallgroup(){
        $group = M("auth_group");
        return $group->select();
    }

    //根据用户ID返回用户组id
    private function getusergroup($id){
        $group = M("auth_group_access");
        return $group->where("uid=".$id)->getField("group_id");
    }

    //根据用户组ID返回用户组名称
    private function getgrouptitle($id){
        $group = M("auth_group");
        return $group->where("id=".$id)->getField("title");
    }

    //根据用户组ID返回用户id的数组
    private function getadminarrbygroupid($id){
        $groupacc =M("auth_group_access");
        return $groupacc->where('group_id='.$id)->getField('uid',true); 
    }

    //根据ID删除用户组
    private function delgroupbyid($id){
        $group = M("auth_group");
        return $group->where("id=".$id)->delete();
    }


    //权限管理列表
    public function rulelist(){
        $class = $this->getallclass();
        for ($i=0; $i < count($class); $i++) { 
            $class[$i]['rules']= $this->getrulesbyclassid($class[$i]['classid']);
        }
        $this->assign('class',$class);
        $this->display("Adminlte/pages/tables/rulelist");
    }

    //添加权限
    public function addrule(){
        $data['title'] = I('post.title');
        $data['name'] = I('post.name');
        $data['classid'] = I('post.classid');
        $rules = M("auth_rule");
        $rules->create($data);
        $res = $rules->add();
        if ($res) {
            $this->ajaxReturn("1");
        }
    }

    //添加权限分类
    public function addruleclass(){
        $data['classname'] = I('post.title');
        $rules = M("auth_rule_class");
        $res = $rules->add($data);
        if ($res) {
            $this->ajaxReturn('1');
        }
    }

    //修改权限分类
    public function editruleclass(){
        $data['classid'] = I('post.id');
        $data['classname'] = I('post.title');
        $ruleclass = M("auth_rule_class");
        $res = $ruleclass->where('classid='.$data['classid'])->save($data);
        if ($res) {
            $this->ajaxReturn('1');
        }
    }

    //修改权限表单
    public function getrule(){
        $id = I('path.2');
        $rule = $this->getrulebyid($id);
        $this->ajaxReturn($rule,'json');
    }

    //修改权限
    public function editrule(){
        $data['id'] = I('post.id');
        $data['classid'] = I('post.classid');
        $data['name'] = I('post.name');
        $data['title'] = I('post.title');
        $rule = M("auth_rule");
        $result = $rule->where('id='.$data['id'])->save($data);
        if ($result) {
            $this->ajaxReturn('1');
        }
    }

    //开启禁用权限
    public function rulestate(){
        $id = I('path.2');
        $rule = M("auth_rule");
        $state = $rule->where('id='.$id)->getField('status');
        if ($state == '1') {
            $res = $rule->where('id='.$id)->setField('status','0');
        }else{
            $res = $rule->where('id='.$id)->setField('status','1');
        }
        if ($res) {
            $this->ajaxReturn("1");
        }
    }

    //删除权限
    public function delrule(){
        $id = I('post.id');
        $rule = M("auth_rule");
        $result = $rule->where("id=".$id)->delete();
        if ($result) {
            $this->ajaxReturn("1");
        }
    }

    //根据ID开启/禁用系统用户
    private function statusadminbyid($id,$status){
        $user = M("admin");
        return $user->where('id='.$id)->setField('status',$status);
    }

    //根据分类ID获取全部权限
    private function getrulesbyclassid($id){
        $rules = M("auth_rule");
        return $rules->where('classid='.$id)->order('id')->select();
    }

    //根据分类ID获取status=1的权限
    private function getstrulesbyclassid($id){
        $rules = M("auth_rule");
        return $rules->where('classid='.$id.' and status=1')->order('id')->select();
    }

    //根据权限ID返回权限
    private function getrulebyid($id){
        $rules = M("auth_rule");
        return $rules->where('id='.$id)->find();
    }

    //获取所有权限列表
    private function getauthrule(){
        $rules = M("auth_rule");
        $condition['status']=1;
        return $rules->where($condition)->select();
    }

    //获取权限分类
    private function getallclass(){
        $class = M("auth_rule_class");
        return $class->select();
    }

    //根据分类ID获取分类名
    private function getclnamebyid($id){
        $class = M("auth_rule_class");
        return $class->where("classid=".$id)->getField("classname");
    }

}